package ihm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import database.Fragment;
import database.Operation;

public class DefinedOperationPanel extends javax.swing.JPanel{
	private JLabel text;
	public DefinedOperationPanel(){
		super();
		Border border= new TitledBorder(new LineBorder(Color.gray,2,true),
                "Defined Operation",TitledBorder.CENTER,TitledBorder.TOP,new Font("Arial Gras", 0, 12));
        this.setBorder(border);
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        text = new JLabel ();
        text.setText ("<HTML> Component<Sub>1</Sub> : op<Sub>1a</sub> ...  op<Sub>1n</sub> <br>" +
        		" ... <br><br>" +
        		"Component<Sub>I</Sub> : op<Sub>Ia</sub> ... op<Sub>In</sub> </HTML>");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(text, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(text, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                .addContainerGap())
        );
	}
	
	public void updateDefinedOperation(Fragment fgt){
		Set cles = fgt.getElementList().keySet();
		Iterator it = cles.iterator();
		String definedOperation="<HTML>";
		while (it.hasNext()){
		   String cle = (String) it.next(); 
		   definedOperation=definedOperation+componentToString(cle, fgt.getElementList().get(cle));
		}
		cles = fgt.getRelationshipList().keySet();
		it = cles.iterator();
		while (it.hasNext()){
			String cle = (String) it.next(); 
			   definedOperation=definedOperation+componentToString(cle, fgt.getRelationshipList().get(cle)); 
		}
		definedOperation+="</HTML>";
		text.setText(definedOperation);
	}

	public String componentToString(String name, ArrayList<Operation> ops){
		String comp=name+":  ";
		for (int i=0;i<ops.size();i++){
			if(i==ops.size()-1)
				comp=comp+ops.get(i)+". ";
			else
				comp=comp+ops.get(i)+",  ";
		}
		comp=comp+"<br>";
		return comp;
	}
}
